package com.jrx.anytxn.investor.controller.api;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.investor.dto.loan.*;
import com.jrx.anytxn.investor.handler.IInvestorLoanHandler;
import com.jrx.anytxn.investor.handler.IInvestorLoanRouteHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Api(tags = "联合贷放款相关接口")
@RestController
@RequestMapping(value = "/api/investor/loan")
public class InvestorLoanController {

    private static final Logger logger = LoggerFactory.getLogger(InvestorLoanController.class);

    @Autowired
    private IInvestorLoanRouteHandler investorLoanRouteHandler;

    @Autowired
    private IInvestorLoanHandler investorLoanHandler;

    @ApiOperation(value = "资金路由受理", notes = "资金路由受理API")
    @PostMapping(value = "/saveFundRoute")
    public TxnRespResult<SaveFundRouteRes> saveFundRoute(@Valid @RequestBody SaveFundRouteReq saveFundRouteReq) throws TxnBizException {
        TxnRespResult result = new TxnRespResult<>();
        logger.debug("资金路由受理开始startTime:{}",result.getStartTime());
        SaveFundRouteRes res = investorLoanRouteHandler.saveFundRoute(saveFundRouteReq);
        result.getSuccess(res);
        logger.debug("资金路由受理结束endTime{}",result.getEndTime());
        return result;
    }

    @ApiOperation(value = "联合贷放款", notes = "联合贷放款API")
    @PostMapping(value = "/loan")
    public TxnRespResult<LoanRes> loan(@Valid @RequestBody LoanReq loanReq) throws Exception {
        TxnRespResult<LoanRes> result = new TxnRespResult<>();
        logger.debug("联合贷放款开始startTime:{}",result.getStartTime());
        result.getSuccess(investorLoanHandler.loan(loanReq));
        logger.debug("联合贷放款结束，返回放款结果endTime{}",result.getEndTime());
        return result;
    }

    @ApiOperation(value = "联合贷放款失败取消订单", notes = "联合贷放款失败取消订单API")
    @PostMapping(value = "/cancelLoan")
    public TxnRespResult<InvestorCancelLoanRes> cancelLoan(@Valid @RequestBody InvestorCancelLoanReq investorCancelLoanReq) throws Exception {
        TxnRespResult<InvestorCancelLoanRes> result = new TxnRespResult<>();
        logger.debug("联合贷放款失败取消订单开始startTime:{}",result.getStartTime());
        result.getSuccess(investorLoanHandler.cancelLoan(investorCancelLoanReq));
        logger.debug("联合贷放款失败取消订单结束 返回订单取消结果endTime{}",result.getEndTime());
        return result;
    }
}
